package com.tesserate.timecontrol.ui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.Format;

import com.tesserate.game.api.graphics.Renderable;
import com.tesserate.game.api.graphics.ResourceManager;
import com.tesserate.timecontrol.clock.Clock;
import com.tesserate.timecontrol.clock.Player;
import com.tesserate.timecontrol.clock.TimeFormat;

public class Lobby extends Canvas implements Renderable{
    
	private static final long serialVersionUID = 1L;
	private GLabel lblLTime;
	private GLabel lblRTime;
	private GLabel lblLMilis;
	private GLabel lblRMilis;
	private GLabel lblLRodada;
	private GLabel lblRRodada;

	private GLabel lblLTempos;
	private GLabel lblRTempos;
	
	private Format timeFormat = new TimeFormat();
	private boolean isLPlayerInTime = false;
	private int state = Clock.READY;
	private Clock clock;
	
	private Player leftPlayer;
	private Player rightPlayer;
	
	public void render(Graphics2D g) {
		g.drawImage(ResourceManager.getImageResource("lobby").getImage(), 0, 0, null);
		g.setColor(Color.black);
		lblLTime.render(g);
		lblRTime.render(g);
		lblLMilis.render(g);
		lblRMilis.render(g);
		lblLRodada.render(g);
		lblRRodada.render(g);
		lblLTempos.render(g);
		lblRTempos.render(g);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		render((Graphics2D)g);
	}
	
	public void update(long elapsedTime) {
		if(isLPlayerInTime)
			leftPlayer.update(elapsedTime);
		else
			rightPlayer.update(elapsedTime);

		if(leftPlayer.getTempo() == 0 || rightPlayer.getTempo() == 0){
			clock.stop();
			return;
		}
		lblLTime.setMsg(timeFormat.format(leftPlayer.getTempo()) );
		lblRTime.setMsg(timeFormat.format(rightPlayer.getTempo()) );
		
		lblLMilis.setMsg( leftPlayer.getMilis() );
		lblRMilis.setMsg( rightPlayer.getMilis() );
		
		lblLRodada.setMsg(leftPlayer.getRodada()+"");
		lblRRodada.setMsg(rightPlayer.getRodada()+"");
		
		StringBuilder lTempos = new StringBuilder();
		for(Long t : leftPlayer.getTempoPorRodada()){
			lTempos.append(t + ", ");
		}
		StringBuilder rTempos = new StringBuilder();
		for(Long t : rightPlayer.getTempoPorRodada()){
			rTempos.append(t + ", ");
		}
		lblLTempos.setMsg((int)leftPlayer.getMedia()+"");
		lblRTempos.setMsg((int)rightPlayer.getMedia()+"");
		
	}

	public void init(){
		clock = new Clock();
		leftPlayer = new Player(10*60*1000);
		rightPlayer = new Player(10*60*1000);
		
		Font f = new Font("Arial Black", Font.PLAIN, 65);
		Font lf = new Font("Arial Black", Font.PLAIN, 20);
		Font mf = new Font("Arial Black", Font.PLAIN, 40);
		Color c = new Color(0,0,0);
		
		lblLTime = new GLabel("0", 375, 145, f, c);
		lblLTime.setAlignment(Component.RIGHT_ALIGNMENT);
		lblRTime = new GLabel("0", 425, 145, f, c);
		lblLTime.setMsg(timeFormat.format(leftPlayer.getTempo()));
		lblRTime.setMsg(timeFormat.format(rightPlayer.getTempo()));
		
		lblLMilis = new GLabel("000", 115, 110, lf, c);
		lblLMilis.setAlignment(RIGHT_ALIGNMENT);
		lblRMilis = new GLabel("000", 685, 110, lf, c);

		lblLRodada = new GLabel("0", 380, 275, mf, c);
		lblLRodada.setAlignment(RIGHT_ALIGNMENT);
		lblRRodada = new GLabel("0", 420, 275, mf, c);

		lblLTempos = new GLabel("0", 380, 345, mf, c);
		lblLTempos.setAlignment(RIGHT_ALIGNMENT);
		lblRTempos = new GLabel("0", 420, 345, mf, c);
	}

	public void passaAVez(){
		if(clock.isReady()) return;
		if(isLPlayerInTime){
			leftPlayer.passaAVez();
		}else{
			rightPlayer.passaAVez();
		}
		
		isLPlayerInTime = !isLPlayerInTime;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return state;
	}
	
	public void start(){
		clock.start();
		leftPlayer.passaAVez();
	}

	public Clock getClock() {
		return clock;
	}

	public void setClock(Clock clock) {
		this.clock = clock;
	}
}
